package common.entities.nonpersistent;

import java.util.Random;

/**
 * a game-question will represent a question during a game (different from DBQuestion that is
 * used to describe a question during creation)
 * 
 * @author Yoyo
 */
public class GameQuestion {

	private String questionStr;
	private String[] answers;
	private int rightAnswerIndex;
	private int questionValue;
	private static int[][] questionOrder = new int[][]{
		new int[]{0,1,2,3}, new int[]{0,1,3,2}, new int[] {0,2,1,3}, new int[] {0,2,3,1},
		new int[]{1,0,2,3}, new int[]{1,0,3,2}, new int[] {2,0,1,3}, new int[] {2,0,3,1},
		new int[] {1,2,0,3},new int[]{1,2,3,0}, new int[] {0,3,1,2}, new int[] {0,3,2,1},
		new int[]{2,1,0,3}, new int[]{2,1,3,0}, new int[] {3,0,1,2}, new int[] {3,0,2,1},		
		new int[]{3,1,0,2}, new int[]{3,1,2,0}, new int[] {3,2,0,1}, new int[] {3,2,1,0},
		new int[]{1,3,0,2}, new int[]{1,3,2,0}, new int[] {2,3,0,1}, new int[] {2,3,1,0}};
	
	
	public GameQuestion(String questionStr, int rightAnswerIndex, int questionValue, String[] answers){
		this.questionValue = questionValue;
		this.rightAnswerIndex = rightAnswerIndex;
		this.answers = answers;
		this.questionStr = questionStr;		
	}

	/**
	 * @return the rightAnswerIndex
	 */
	/*Note: the only reason rightAnswerIndex is included in a GameQuestion Object
	 * is for implementing 'GetAnswer' button for administrators*/
	public int getRightAnswerIndex() {
		return rightAnswerIndex;
	}

	
	/**
	 * @return the questionValue (game points value)
	 */
	public int getQuestionValue() {
		return questionValue;
	}

	
	/**
	 * @return the answers
	 */
	public String[] getAnswers() {
		return answers;
	}
	
	
	
	/**
	 * @sets the value of the question
	 */
	public void setQuestionValue(int value) {
		this.questionValue = value;
	}
	

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return questionStr;
	}
	
	
	/**
	 * changes the order of the answers
	 * **/
	public void changeOrderAnswers(){
		Random gen = new Random();
		int randNum = gen.nextInt(24);
		int[] ord = questionOrder[randNum];
		String[] newAnswers = new String[4];
		boolean changedRightAnswer = false;
		for (int i=0; i<4; i++){
			newAnswers[i] = answers[ord[i]];
			if (ord[i] == rightAnswerIndex && !changedRightAnswer){
				changedRightAnswer = true;
				rightAnswerIndex = i;
			}
		}
		answers = newAnswers;
	}
	
}
